package com.jhd.fuzhou.util;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;

import java.util.List;

public class OpenWps {

    public static boolean checkWps(Context context){
        Intent intent = context.getPackageManager().getLaunchIntentForPackage("cn.wps.moffice_eng");//WPS个人版的包名
        if (intent == null) {
            return false;
        } else {
            return true;
        }
    }
    public static void openWps(Context context,String fileUrl){
        //appBack(context);
        /*Intent intent2 = context.getPackageManager().getLaunchIntentForPackage("com.jhd.fuzhou");
        String classNameString = intent2.getComponent().getClassName();//得到app类名*/
        Intent intent = context.getPackageManager().getLaunchIntentForPackage( "cn.wps.moffice_eng");
        Bundle bundle = new Bundle();
        bundle.putString("OpenMode", "ReadOnly");
        intent.setData(Uri.parse(fileUrl));//这里采用传入文档的在线地址进行打开，免除下载的步骤，也不需要判断安卓版本号
        //关闭后删除打开文件
        intent.setAction(android.content.Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        //intent.setComponent(new ComponentName("com.jhd.fuzhou", classNameString));
        intent.putExtras(bundle);
        context.startActivity(intent);
    }
    private static void appBack(Context context) {
        try {
            ActivityManager mAm = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            //获得当前运行的task
            List<ActivityManager.RunningTaskInfo> taskList = mAm.getRunningTasks(1);
            //找到当前应用的task，并启动task的栈顶activity，达到程序切换到前台
            for (ActivityManager.RunningTaskInfo rti : taskList) {
                if (rti.topActivity.getPackageName().equals(context.getPackageName())) {
                    Intent LaunchIntent = new Intent(Intent.ACTION_MAIN);
                    ComponentName cn = new ComponentName(context.getPackageName(), rti.topActivity.getClassName());
                    LaunchIntent.setComponent(cn);
                    context.startActivity(LaunchIntent);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
